/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listener;

import controller.Login;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.Map;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *
 * @author vt
 */
public class CheckLogin implements PhaseListener {

    @Override
    public void afterPhase(PhaseEvent event) {
        //System.out.println("END :  "  +  event.getPhaseId());
            FacesContext context = event.getFacesContext();
            NavigationHandler navi = context.getApplication().getNavigationHandler();
            String viewid = context.getViewRoot().getViewId();
            HttpSession session = (HttpSession) context.getExternalContext().getSession(false);
            System.out.println("Trang Hien Tai :  "  +  viewid);
            
            if(session != null){
                System.out.println("##  SESSION ID : #  " + session.getId());
            }
            
                 if(!isLogged(context) ){  // neu chua login
                      if(!viewid.equals("/login.xhtml")){ // trang hien tai khong phai trang login
                                navi.handleNavigation(context, null, "/login.xhtml");
                      } 
               }  
}
    @Override
    public void beforePhase(PhaseEvent pe) {
       // System.out.println("START  :  "  +  pe.getPhaseId());
        // HttpSession session = ((HttpServletRequest) (cont.getExternalContext().getRequest())).getSession(true);
             //Enumeration e = session.getAttributeNames();
         /* while (e.hasMoreElements())
                {
                  String attr = (String)e.nextElement();
                  System.err.println("      attr  = "+ attr);
                   Login  value = (Login) session.getAttribute(attr);
                  System.err.println("      value = "+ value ); 
                }*/
    }
    private boolean isLogged(FacesContext cont) 
     {
    // Login b = (Login)((HttpServletRequest) (cont.getExternalContext().getRequest())).getSession().getAttribute("loginBean");
          Map m = FacesContext.getCurrentInstance().getExternalContext().getSessionMap();
          HttpSession session = (HttpSession) cont.getExternalContext().getSession(false); // khong tao neu no da ton tai
          if(session != null )
         {  
                       String isLog = (String) session.getAttribute(Login.SESSION_NAME_LOGIN);
                     if( isLog !=null && isLog.equals("1") ){ // neu da nhan nut login roi. 
                             System.out.println(" LOGINED AT " +  (new SimpleDateFormat("dd/MM/yyyy HH: mm ss ")).format(new Date())  );
                             return true;
                   }  
       }    
return false;
}
    @Override
    public PhaseId getPhaseId() {
     return  PhaseId.RESTORE_VIEW;
    }
    
}
